﻿using KeDa2025_.Utils;
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace KeDa2025_
{
    public partial class denglu : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            #region 免登录验证
            if (Request.Cookies["account"] != null & Request.Cookies["pwd"] != null)
            {
                string account = Request.Cookies["account"].Value;
                string pwd = Request.Cookies["pwd"].Value;

                //DataSet ds = DbHelper.Query($@"select u.*,c.CityName as city_name  from users u inner join S_City c on c.CityID=u.city_id
                //                           where u.account = '{account}' and u.pwd = '{pwd}'");
               
                string sql = string.Format("select * from users where account='{0}'  and  pwd='{1}'", account, pwd);
                MySqlConnection connection = new MySqlConnection("server=localhost;user id=zmy2;password=zmy20020525;database=forum;");
                MySqlDataAdapter sqlDataAdapter = new MySqlDataAdapter(sql, connection);
                DataSet ds = DbHelper.Query($@"select u.*,c.name as name  from users u inner join city c on c.id=u.city_id
            where u.account = '{account}' and u.pwd = '{pwd}'");
                //DataSet ds = new DataSet();
                // 填充数据集
                sqlDataAdapter.Fill(ds);


                if (ds.Tables[0].Rows.Count > 0)
                {
                    //4.存session
                    Session["User"] = ds.Tables[0].Rows[0];//把当前登录人的整行字段信息全部存到session
                    //5.跳页
                    Response.Redirect("index.aspx");
                }
            }
        }

        protected void btLogin_Click(object sender, EventArgs e)
        {
            string account = txtAccount.Text.Trim();
            string pwd = txtPwd.Text.Trim();
            string code = Session["Code"].ToString();
            string userCode = txtCode.Text.Trim();
           // 验证码是否正确
            if (userCode != code)
            {
                Response.Write("<script>alert('验证码错误')</script>");
                return;
            }
            //账号密码是否正确
            //////DataSet ds = DbHelper.Query($"select *  from users where account='{account}' and pwd='{pwd}'");
            string sql = string.Format($@"select u.*,c.name as name  from users u inner join city c on c.id=u.city_id
            where u.account = '{account}' and u.pwd = '{pwd}'");
            //string sql = string.Format("select * from users where account='{0}'  and  pwd='{1}'", account, pwd);
            MySqlConnection connection = new MySqlConnection("server=localhost;user id=zmy2;password=zmy20020525;database=forum;");
            MySqlDataAdapter sqlDataAdapter = new MySqlDataAdapter(sql, connection);
            DataSet ds = new DataSet();
            // 填充数据集
            sqlDataAdapter.Fill(ds);
            if (ds.Tables[0].Rows.Count == 0)
            {
                Response.Write("<script>alert('账号密码错误')</script>");
                return;
            }

            var typeObj = ds.Tables[0].Rows[0]["type"];
            int type = typeObj != DBNull.Value ? Convert.ToInt32(typeObj) : -1; // 默认值为 -1
            if (type == 0)
            {
                Response.Write("<script>alert('请等待管理人员审核')</script>");
                return;
            }
            //验证码 账号密码 审核都是通过
            Session["User"] = ds.Tables[0].Rows[0];//将用户整行信息存入到Session
            if (cbxRMe1.Checked)
            {
                HttpCookie accoutCookie = new HttpCookie("account");
                accoutCookie.Value = account;
                accoutCookie.Expires = DateTime.Now.AddMonths(1);
                HttpCookie pwdCookie = new HttpCookie("pwd");
                pwdCookie.Value = account;
                pwdCookie.Expires = DateTime.Now.AddMonths(1);
                Response.Cookies.Add(accoutCookie);
                Response.Cookies.Add(pwdCookie);
            }
            Response.Redirect("index.aspx");
        }
        #endregion

        protected void cbxRMe1_CheckedChanged(object sender, EventArgs e)
        {

        }
    }
}
